import java.util.*;

public class Main {

    private TrieNode root;

    public Main() {
        root = new TrieNode();
    }

    public void insert(String word) {
        if (word == null) {
            return;
        }
        char[] cs = word.toCharArray();
        int index = 0;
        TrieNode node = root;
        for (int i = 0; i < cs.length; i++) {
            index = cs[i] - 'a';
            if (node.map[index] == null) {
                node.map[index] = new TrieNode();
            }
            node = node.map[index];
            node.path++;
        }
        node.end++;
    }

    public boolean search(String word) {
        if (word == null) {
            return false;
        }
        char[] cs = word.toCharArray();
        int index = 0;
        TrieNode node = root;
        for (int i = 0; i < cs.length; i++) {
            index = cs[i] - 'a';
            if (node.map[index] == null) {
                return false;
            }
            node = node.map[index];
        }
        return node.end != 0;
    }

    public void delete(String word) {
        if (search(word)) {
            char[] cs = word.toCharArray();
            int index = 0;
            TrieNode node = root;
            for (int i = 0; i < cs.length; i++) {
                index = cs[i] - 'a';
                if (node.map[index].path-- == 1) {
                    node.map[index] = null;
                    return;
                }
                node = node.map[index];
            }
            node.end--;
        }
    }

    public int prefixNumber(String pre) {
        if (pre == null) {
            return 0;
        }
        char[] cs = pre.toCharArray();
        int index = 0;
        TrieNode node = root;
        for (int i = 0; i < cs.length; i++) {
            index = cs[i] - 'a';
            if (node.map[index] == null) {
                return 0;
            }
            node = node.map[index];
        }
        return node.path;
    }
}

class TrieNode {
    public int path;
    public int end;
    public TrieNode[] map;

    public TrieNode() {
        path = 0;
        end = 0;
        map = new TrieNode[26];
    }
}
